package projectodedalo.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import projectodedalo.modelo.Discusion;
import projectodedalo.modelo.Secuencia;
import projectodedalo.negocio.GestionDiscusion;
import projectodedalo.negocio.GestionIdea;

/**
 * Servlet implementation class CargarDetalleIdeasServlet
 */
@WebServlet("/CargarDetalleIdeasServlet")
public class CargarDetalleIdeasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CargarDetalleIdeasServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("doPost CargarDetalleIdeasServlet");
		String xtest = request.getParameter("test");
		String xsubtitulo = request.getParameter("subTitulo");
		int xcodigo = Integer.parseInt(request.getParameter("codigo"));
		
		//Variables de sesion
		HttpSession session = request.getSession();
		session.setAttribute("CODIGO_IDEA", xcodigo);
		session.setAttribute("SUB_TITULO", xsubtitulo);
		
		System.out.println(xtest+" "+xsubtitulo+" "+xcodigo);
		if (xtest.equals("1"))
			request.setAttribute("test", "on");
		else
			request.setAttribute("test", "off");
		
		try {
			GestionDiscusion discusion = new GestionDiscusion();
			request.setAttribute("lstDiscusion",discusion.listarDiscusionxCodigo(xcodigo));
			ArrayList<Discusion> beanDiscusion = (ArrayList<Discusion>) request.getAttribute("lstDiscusion");
			if (beanDiscusion.size()==0){
				request.setAttribute("codigoMsg", "ERROR");
				request.setAttribute("servletMsg", "No se ha encontrado discusiones para esta idea...");
			} else {
				request.setAttribute("codigoMsg", "OK");
				request.setAttribute("servletMsg", "Comentario agregado correctamente");
			}
			/*ArrayList<Discusion> beanDiscusion = (ArrayList<Discusion>) request.getAttribute("lstDiscusion");
			Iterator<Discusion> discusionIterator = beanDiscusion.iterator();
			while(discusionIterator.hasNext()){
				Discusion elemento = discusionIterator.next();
				System.out.print(elemento.getCodSecuencia() +" / ");
				
				Iterator<Secuencia> secuenciaIterator = (Iterator<Secuencia>) elemento.getSecuencia().iterator();
				while(secuenciaIterator.hasNext()){
					Secuencia elemsec = secuenciaIterator.next();
					System.out.println(elemsec.getComentario());
				}
				
			}*/
			
		} catch (Exception e) {
			// TODO: handle exception
			request.setAttribute("servletMsg", e.getMessage());
		}
		
		request.setAttribute("subtitulo", xsubtitulo);
		RequestDispatcher rd = request.getRequestDispatcher("detalleForumIdea.jsp");
		rd.forward(request, response);
	}

}
